<!DOCTYPE html >
<html>
<head>
<meta name="Generator" content="TPshop v1.1" />
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<title>添加话术</title>
<meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=0"/>
<!-- <link rel="stylesheet" href="__ROOT__ /AI_radar/css/footer.css"> -->
<link rel="stylesheet" href="__ROOT__ /AI_radar/css/font_my/iconfont.css">
<script type="text/javascript" src="__ROOT__ /AI_radar/js/token.js"></script>
<style type="text/css">
	body,html{
		width: 100%;
		height: 100%;
		margin: 0;
		padding: 0;
		overflow-x: hidden;
		color:#555555;
		background: #eee;
	}
	p{
		margin: 0;
	}
	.bian{
		width: calc(100% - 20px);
		padding: 0 10px;
	}
	.trickAddYin{
		display: flex;
		background: #eee;
		line-height: 50px;
	}
	.trickAddYinL{
		width: calc(100% - 90px);
		font-weight: bold;
		font-size: 16px
	}
	.trickAddYinR{
		width: 90px;
		text-align: right;
	}
	textarea{
		width: 100%;
		height: 80px;
		resize: none;
		border:none;
	}
	.yanse{
		background: #fff
	}
	
	.trickAddBot{
		padding: 10px 10px;
	}
	input{
		width: 100%;
		height: 35px;
		border:none;
		outline:none;
	}
	.trickAddBtn{
		width: calc(100% - 20px);
		line-height: 35px;
		margin: 30px 10px;
		text-align: center;
		color: #FFF;
		background: #4f81d4;
	}
	


</style>

</head>
<body>
	{include file="public/wx_share" /}
	<div class="trickAdd">
		<div class="bian trickAddYin">
			<div class="trickAddYinL">话术内容</div>
			<div class="trickAddYinR"><span class="AddNK">0</span>/300</div>
		</div>
		<div class="bian yanse">
			<textarea name="" id="aa" placeholder="请输入话术内容" maxlength="300"></textarea>
		</div>
		<div class="bian trickAddYin">
			<div class="trickAddYinL">关键字</div>
			<div class="trickAddYinR"><span class="AddFK">0</span>/8</div>
		</div>
		<div class="bian yanse trickAddBot">
			<input type="text" placeholder="请输入关键字" class="trickAddF" maxlength="8">
		</div>
		<div class="trickAddBtn" cid='0'>保存</div>
	</div>

<script src='__ROOT__ /AI_radar/js/jquery.js'></script>
<script type="text/javascript">
	var test = window.location.href;
    var jin1 = test.indexOf('?id=');
	var jin0 = test.indexOf('&corpid=');

    var jin3 = jin1 + 4;
    var jin = test.slice(jin3,jin0);
    console.log(jin)
    if(jin != 0){
    	updatdAjax(jin)
    }
	$('#aa').bind('input propertychange', function() {
        var $this = $(this);
        var text_length = $this.val().length;//获取当前文本框的长度
        $(".AddNK").html(text_length)
        if(text_length >300){
        	alert("最多只能输入300个字符")
        }
    });
    $('.trickAddF').bind('input propertychange', function() {
        var $this = $(this);
        var text_length = $this.val().length;//获取当前文本框的长度
        $(".AddFK").html(text_length)
        if(text_length >8){
        	alert("最多只能输入8个字符")
        }
    });
    $(".trickAddBtn").click(function(){
    	var aa = $('#aa').val()
    	var trickAddF = $('.trickAddF').val()
    	if(aa == ''){
    		alert("话术内容不能为空");
    		return false;
    	}
    	if(trickAddF == ''){
    		alert("关键字不能为空");
    		return false;
    	}
    	var arr = [];
    	// console.log($(".trickAddBtn").attr("cid"))
    	// console.log(aa,trickAddF)
    	if($(".trickAddBtn").attr("cid") == 0){

    		addAjax(aa,trickAddF)
    	}else{
    		updatasAjax(aa,trickAddF,$(".trickAddBtn").attr("cid"))
    	}
    })
    //添加
    function addAjax(aa,trickAddF){
    	console.log("添加")
    	$.ajax({
	      url:window.urls.url + "/my/trick/add",
	      type:"POST",
	      contentType: "application/json",
	      dataType:"json",
	      data:JSON.stringify({
	        token:window.tokens.token_boss,
	        content:aa,
	        keyword:trickAddF
	      }),
	      success:function(res){
	        console.log(res)
	        // console.log(res.data)
	        if(res.data == 1){
	        	alert("添加话术成功")
	        }else{
	        	alert("添加话术失败，请重试")
	        }
	   

	        window.location.href=window.urls.url + `/my/trick?id=0&corpid=${localStorage.getItem("CRM_corpId")}`
	      },
	      error:function(res){
	        console.log(res)
	      }
  		})
    }
    //编辑
    function updatdAjax(num){
    	console.log(num)
    	$.ajax({
	      url:window.urls.url + "/my/trick/get",
	      type:"POST",
	      contentType: "application/json",
	      dataType:"json",
	      data:JSON.stringify({
	        token:window.tokens.token_boss,
	        id:num
	      }),
	      success:function(res){
	        console.log(res)
	        // console.log(res.data)
	        $("#aa").val(res.data.content)
	        $(".AddNK").html(res.data.content.length)

	        $(".trickAddF").val(res.data.keyword)
	        $(".AddFK").html(res.data.keyword.length)
	        $(".trickAddBtn").attr("cid",res.data.id)

	      },
	      error:function(res){
	        console.log(res)
	      }
  		})
    }
    //修改
    function updatasAjax(aa,trickAddF,nums){
    	console.log("修改")
    	$.ajax({
	      url:window.urls.url + "/my/trick/update",
	      type:"POST",
	      contentType: "application/json",
	      dataType:"json",
	      data:JSON.stringify({
	        token:window.tokens.token_boss,
	        id:nums,
	        content:aa,
	        keyword:trickAddF
	      }),
	      success:function(res){
	        console.log(res)
	        // console.log(res.data)
	        if(res.data == 1){
	        	alert("修改话术成功")
	        }else{
	        	alert("修改话术失败，请重试")
	        }
	        window.location.href=window.urls.url + `/my/trick?id=0&corpid=${localStorage.getItem("CRM_corpId")}`
	      },
	      error:function(res){
	        console.log(res)
	      }
  		})
    }
	// console.log($)

</script>
</body>
</html>